<?php
class Library_Model_KhachHangMapper  extends Library_Model_DataMapperAbstract{
	private static $_instance = null;
	private $_paginator = null ;
	
/* 	public function searchIdKhachHang($id)
	{
		$db = $this->getDb();
		$selectKhachhang = $db->select()
		->from('tbl_khachhang')
		->join('tbl_dangkythe', 'tbl_dangkythe.MaKhachHang = tbl_khachhang.MaKhachHang')
		->where('tbl_khachhang.MaKhachHang = ?',$id);
		$khachhang = $db->fetchRow($selectKhachhang);
	
		return $this->_populate($khachhang);
	
	} */
/* 	public function searchMaKhachHang($id){
		$db = $this->getDb();
		$selectKhachhang = $db->select()
		->from('tbl_khachhang')
		->join('tbl_chitietthe', 'tbl_chitietthe.MaKhachHang = tbl_khachhang.MaKhachHang')
		->where('tbl_khachhang.MaKhachHang = ?',$id);
		$khachhang = $db->fetchRow($selectKhachhang);
		
		return $this->_populate($khachhang);
	} */
	
	public function searchById($id)
	{
		$db = $this->getDb();
		$select = $db->select()
		->from('tbl_khachhang')->where('MaKhachHang = ?', $id);
		$khachhang = $db->fetchRow($select);
		return $this->_populate($khachhang);
	}
	
	public function khachhangList()
	{
		$db = $this->getDb();
		$selectKhachhang = $db->select()
												->from('tbl_khachhang');
		$khachhangs = $db->fetchAll($selectKhachhang);
	
		$khachhangObjArray = array();
		foreach($khachhangs as $khachhang){
			$khachhangObjArray[] = $this->_populate($khachhang);
		}
	
		return $khachhangObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_KhachHang();
		$obj->setId($data['MaKhachHang']);
		$obj->setHovaten($data['HoVaTen']);
		$obj->setNgaysinh($data['NgaySinh']);
		$obj->setDiachithuongtru($data['DiaChiThuongTru']);
		$obj->setDiachilienlac($data['DiaChiLienLac']);
		$obj->setDienthoai($data['DienThoai']);
		$obj->setCmnd($data['CMND']);
		$obj->setNgaycap($data['NgayCap']);
		$obj->setNoicap($data['NoiCap']);
		$obj->setTencoquan($data['TenCoQuan']);
		$obj->setDiachicoquan($data['DiaChiCoQuan']);
		$obj->setDienthoaicoquan($data['DienThoaiCoQuan']);
		
		return $obj;
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data =array(
				//'MaKhachHang' 		=> $obj->getId(),
				'HoVaTen'				=>$obj->getHovaten(),
				'NgaySinh'				=>$obj->getNgaysinh(),
				'DiaChiThuongTru'	=>$obj->getDiachithuongtru(),
				'DiaChiLienLac'		=>$obj->getDiachilienlac(),
				'DienThoai'				=>$obj->getDienthoai(),				
				'CMND'					=>$obj->getCmnd(),
				'NgayCap'				=>$obj->getNgaycap(),
				'NoiCap'					=>$obj->getNoicap(),
				'TenCoQuan'			=>$obj->getTencoquan(),
				'DiaChiCoQuan'		=>$obj->getDiachicoquan(),
				'DienThoaiCoQuan'		=>$obj->getDienthoaicoquan()
				);
		$db->insert('tbl_khachhang',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data =array(	
				//'MaKhachHang' 			=> $obj->getId(),
				'HoVaTen'					=>$obj->getHovaten(),
				'NgaySinh'					=>$obj->getNgaysinh(),
				'DiaChiThuongTru'		=>$obj->getDiachithuongtru(),
				'DiaChiLienLac'			=>$obj->getDiachilienlac(),
				'DienThoai'					=>$obj->getDienthoai(),				
				'CMND'						=>$obj->getCmnd(),
				'NgayCap'					=>$obj->getNgaycap(),
				'NoiCap'						=>$obj->getNoicap(),
				'TenCoQuan'				=>$obj->getTencoquan(),
				'DiaChiCoQuan'			=>$obj->getDiachicoquan(),
				'DienThoaiCoQuan'	=>$obj->getDienthoaicoquan()	
		);
		$where = $db->quoteInto('MaKhachHang = ?', $obj->getId());
		$db->update('tbl_khachhang',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaKhachHang = ?',$id);
		$db->delete('tbl_khachhang',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
}